﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.Caching;
using MySQL.Db;

namespace Gollie.DataTables
{
	public class MyCreditsItem
	{
		public string Bedrijfsnaam { get; set; }
		public string Straat { get; set; }
		public string Plaats { get; set; }
		public string Website { get; set; }
		public decimal Saldo { get; set; }
		public uint VendorId { get; set; }
		public decimal FutureSaldo { get; set; }
	}

	public class MyCredits : BaseBindable<MyCreditsItem> 
	{
		internal SortedDictionary<string, uint> vendors = new SortedDictionary<string, uint>() { { "<Alles>", 0 } };

		public MyCredits(uint userId)
		{
			ResultSet resultSet = Database.Queries.GetUserSaldi(userId);

			this.AddDetails(resultSet);
		}

		private void AddDetails(ResultSet resultSet)
		{
			int numRows = resultSet.Count;

			for (int i = 0; i < numRows; i++)
			{
				string vendor = resultSet.GetString(i, "name");
				uint id = resultSet.GetUInt32(i, "id");

				this.Add(new MyCreditsItem()
				{
					Bedrijfsnaam = vendor,
					Straat = resultSet.GetString(i, "street"),
					Plaats = resultSet.GetString(i, "city"),
					Website = resultSet.GetString(i, "website"),
					Saldo = resultSet.GetDecimal(i, "saldo"),
					VendorId = id,
					FutureSaldo = resultSet.GetDecimal(i, "futuresaldo")
				});
				
				if (vendors.ContainsKey(vendor) == false)
					vendors[vendor] = id;
			}
		}
	}
}